<template>
  <div>
    <a-space>
      <a-button class="editable-add-btn" @click="handleAdd">新增</a-button>
      <a-button class="editable-add-btn" @click="handleSave">保存</a-button>
      <a-button class="editable-add-btn" @click="handleDelete">删除</a-button>
    </a-space>
    <a-table bordered :data-source="dataSource" :columns="columns" :pagination="false">
      <template slot="name" slot-scope="text, record">
        <dmt-value-item :text="text" @change="onCellChange(record.key, 'name', $event)"></dmt-value-item>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-popconfirm
          v-if="dataSource.length"
          title="Sure to delete?"
          @confirm="() => onDelete(record.key)"
        >
          <a href="javascript:;">删除</a>
        </a-popconfirm>
      </template>
    </a-table>
  </div>
</template>
  <script>
import DmtValueItem from './DmtValueItem'

export default {
  name: 'DmtValue',
  components: {
    DmtValueItem
  },
  data() {
    return {
      dataSource: [],
      count: 0,
      columns: [
        {
          title: '代码',
          dataIndex: 'code'
        },
        {
          title: '名称',
          dataIndex: 'name',
          width: '30%',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '值',
          dataIndex: 'value'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  methods: {
    onCellChange(key, dataIndex, value) {
      const dataSource = [...this.dataSource]
      const target = dataSource.find(item => item.key === key)
      if (target) {
        target[dataIndex] = value
        this.dataSource = dataSource
      }

      this.$emit('onValueUpdate', this.dataSource)
    },
    onDelete(key) {
      const dataSource = [...this.dataSource]
      this.dataSource = dataSource.filter(item => item.key !== key)
      this.$emit('onValueUpdate', this.dataSource)
    },
    handleAdd() {
      const { count, dataSource } = this
      const newData = {
        key: count,
        name: `NAME${count}`,
        code: 32,
        value: `VALUE_${count}`
      }
      this.dataSource = [...dataSource, newData]
      this.count = count + 1

      this.$emit('onValueUpdate', this.dataSource)
    },
    handleSave() {},
    handleDelete() {}
  }
}
</script>
  <style>
.editable-cell {
  position: relative;
}

.editable-cell-input-wrapper,
.editable-cell-text-wrapper {
  padding-right: 24px;
}

.editable-cell-text-wrapper {
  padding: 5px 24px 5px 5px;
}

.editable-cell-icon,
.editable-cell-icon-check {
  position: absolute;
  right: 0;
  width: 20px;
  cursor: pointer;
}

.editable-cell-icon {
  line-height: 18px;
  display: none;
}

.editable-cell-icon-check {
  line-height: 28px;
}

.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}

.editable-cell-icon:hover,
.editable-cell-icon-check:hover {
  color: #108ee9;
}

.editable-add-btn {
  margin-bottom: 8px;
}
</style>
